//
//  AppDelegate.swift
//  微博
//
//  Created by Mstarc on 2016/11/28.
//  Copyright © 2016年 Mstarc. All rights reserved.
//

import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        //#available 是检测设备版本 如果是10.0以上
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.carPlay,.sound]) { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {
            // Fallback on earlier versions
            //获取用户授权显示通知[上方的提示条,声音,bageNumber]
            let notifiSettig = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            
            application.registerUserNotificationSettings(notifiSettig)
        }

        
//        sleep(2)
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        loadAppInfo()
        return true
    }

   

}

extension AppDelegate{
    
     func loadAppInfo(){
        //1.模拟异步
        DispatchQueue.global().async {
            //1>url
            
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            //2>data
            let data = NSData(contentsOf: url!)
            //3>写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            //直接保存在沙盒,等待下次程序启动!
            data?.write(toFile: jsonPath, atomically: true)
            print("应用程序加载完毕\(jsonPath)")
            
        }
        
        
        
        
        
    }
    
    
}








